﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ArkEC.CoreCom.CacheStore
{
    /// <summary>
    /// 通用缓存辅助处理类
    /// </summary>
    public class CacheHelper
    {
        #region 属性

        /// <summary>
        /// 缓存存放空间
        /// </summary>
        private static Dictionary<string, object> mCacheStorage = new Dictionary<string,object>();

        #endregion

        #region 添加或修改缓存值/条目

        /// <summary>
        /// 添加或修改缓存值
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="cacheLifeCycle"></param>
        /// <param name="value"></param>
        public static void SetItem(string cacheName, object value)
        {
            if (mCacheStorage.ContainsKey(cacheName))
            {
                mCacheStorage[cacheName] = value;
            }
            else
            {
                mCacheStorage.Add(cacheName, value);
            }
        }

        /// <summary>
        /// 添加或修改缓存条目
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="cacheLifeCycle"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetItem(string cacheName, string key, object value)
        {
            Dictionary<string, object> dict = null;
            if (mCacheStorage.ContainsKey(cacheName))
            {
                dict = mCacheStorage[cacheName] as Dictionary<string, object>;
            }
            else
            {
                dict = new Dictionary<string, object>();
            }

            if (dict.ContainsKey(key))
            {
                dict[key] = value;
            }
            else
            {
                dict.Add(key, value);
            }
        }

        #endregion

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="cacheName"></param>
        public static void RemoveCache(string cacheName)
        {
            mCacheStorage.Remove(cacheName);
        }

        /// <summary>
        /// 清除缓存条目（只支持数据字典结构）
        /// </summary>
        /// <param name="cacheName"></param>
        /// <param name="key"></param>
        public static void RemoveCacheItem(string cacheName, string key)
        {
            Dictionary<string, object> cacheObj = mCacheStorage[cacheName] as Dictionary<string, object>;
            if (cacheObj != null)
            {
                cacheObj.Remove(key);
            }
        }

        /// <summary>
        /// 获取缓存条目
        /// </summary>
        /// <param name="cacheType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetItem(CacheType cacheType, string key)
        {
            Dictionary<string, object> cacheObj = null;
            if (!mCacheStorage.ContainsKey(cacheType.CacheName))
            {
                //自动获取数据源
                cacheObj = cacheType.ReadCache();
                mCacheStorage.Add(cacheType.CacheName, cacheObj);
            }
            else
            {
                cacheObj = mCacheStorage[cacheType.CacheName] as Dictionary<string, object>;
            }

            object obj = null;
            if (cacheObj.ContainsKey(key))
            {
                obj = cacheObj[key];
            }

            return obj;
        }
    }
}
